package com.xian.spark.jdbc;

import org.apache.spark.SparkConf;
import org.apache.spark.SparkContext;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.DataFrameReader;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.apache.spark.sql.hive.HiveContext;

import java.util.HashMap;

/**
 * spark 2.x 版本
 */
public class ReadMysql2 {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf();
        conf.setMaster("local");
        conf.setAppName("ReadMysql");

        SparkContext sc = new SparkContext(conf);

        SparkSession session = new SparkSession(sc);
        DataFrameReader read = session.read();



        HashMap<String, String> options = new HashMap<>();
        options.put("driver","com.mysql.jdbc.Driver");
        options.put("url","jdbc:mysql://r71:3306/testdb?characterEncoding=utf8&useSSL=TRUE");
        options.put("user","root");
        options.put("password","123456");
        options.put("dbtable","person");

        Dataset<Row> tableDS = read.format("jdbc").options(options).load();

        tableDS.printSchema();
        tableDS.show();

        sc.stop();



    }
}
